//
//  WFSavingsMethodDetailViewController.m
//  WeiFang
//
//  Created by WeiFang on 2023/10/14.
//  Copyright © 2023 WeiFang. All rights reserved.
//

#import "WFSavingsMethodDetailViewController.h"

#import "WFSavingsMethodDetailListCollectionViewCell.h"

#import "WFSavingsMethodDetailToolView.h"

@interface WFSavingsMethodDetailViewController ()

@property (nonatomic,copy) NSArray *dataAry;
@property (nonatomic,strong) WFSavingsMethodDetailToolView *savingsMethodDetailToolView;

@end

@implementation WFSavingsMethodDetailViewController

#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationItemTitleViewWithTitle:@"365存钱法"];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"WFSavingsMethodDetailListCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"WFSavingsMethodDetailListCollectionViewCell"];
        
    self.collectionView.frame = CGRectMake(0,self.navigationBarHeight + 10, kScreenWidth, kScreenHeight - 318 - (self.navigationBarHeight + 10));
    
    [self requestData];
    
    self.savingsMethodDetailToolView.frame = CGRectMake(28,kScreenHeight - 171 - 50, kScreenWidth - 28*2, 171);
    [self.view addSubview:self.savingsMethodDetailToolView];
}

- (void)requestData {
    NSArray *dataAry = [kDataManager getEatArray];
    self.dataAry = [NSMutableArray arrayWithArray:dataAry];
    [self refreshData];
}

#pragma mark - cellDataSource
    
- (NSArray *)cellDataSource {
    if (!self.dataSource) {
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:4];
        
        __block NSMutableArray *subarr = nil;
        __block NSDictionary *dict = nil;
  
        if (self.dataAry.count > 0) {

            subarr = [NSMutableArray arrayWithCapacity:1];
                     
            for (int i = 1; i <= 365; i++) {
                NSInteger n = i;
                NSInteger an = kUserManager.userManager.startValue.integerValue + (n - 1) * kUserManager.userManager.dailyIncrease.integerValue;
                
                dict =  @{@"class":WFSavingsMethodDetailListCollectionViewCell.class,
                          @"width":@(kScreenWidth/15),
                          @"height":@(22),
                          @"data":[WFSavingsMethodDetailListCollectionViewCellModel ittemModelWithTitle:[NSString stringWithFormat:@"%ld",an] index:i-1]};
                [subarr addObject:dict];
            }
                                    
            [arr addObject:subarr];
        }

        self.dataSource = arr;
    }
    return self.dataSource;
}

#pragma mark - UICollectionView 代理
    
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.cellDataSource.count;
}
    
//定义展示的UICollectionViewCell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSArray *arr = self.cellDataSource[section];
    return [arr count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *section = self.cellDataSource[indexPath.section];
    NSDictionary *cellDict = section[indexPath.row];
    
    Class classs = cellDict[@"class"];
    
    BaseCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(classs) forIndexPath:indexPath];
    
    NSNumber *delFlag = cellDict[@"delegate"];
    
    id delegate = nil;
    
    if (delFlag && delFlag.boolValue) {
        delegate = self;
    }
    
    [cell setData:cellDict delegate:delegate];
    
    return cell;
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *section = self.cellDataSource[indexPath.section];
    NSDictionary *cellDict = section[indexPath.row];
    float width = [cellDict[@"width"] floatValue];
    float height = [cellDict[@"height"] floatValue];
    return CGSizeMake(width, height);
}
    
// 设置每个cell上下左右相距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(1, 1, 1, 1);
}

//设置最小行间距，也就是前一行与后一行的中间最小间隔
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 1;
}
    
// 设置最小列间距，也就是左行与右一行的中间最小间隔
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 1;
}

#pragma mark - refreshData
    
/**
 *  刷新tableView
 */
- (void)refreshData {
    self.dataSource = nil;
    [self.collectionView reloadData];
}

#pragma mark - 懒加载

//搜索
- (WFSavingsMethodDetailToolView *)savingsMethodDetailToolView {
    if (!_savingsMethodDetailToolView) {
        _savingsMethodDetailToolView = [WFSavingsMethodDetailToolView instanceView];
        [_savingsMethodDetailToolView setData:nil delegate:nil];
    }
    return _savingsMethodDetailToolView;
}

@end

